//
//  AbstractBaseERPViewController.m
//  ERP
//
//  Created by goldeney on 15/4/27.
//  Copyright (c) 2015年 ShangHai Goldeneye Techonology Co.,Ltd. All rights reserved.
//

#import "AbstractBaseERPViewController.h"
#import "REMenu.h"

@interface AbstractBaseERPViewController ()

@property (strong, readwrite, nonatomic) REMenu *stationMenu;
@property (nonatomic, strong) NSMutableArray *rightItems;

@property (nonatomic, strong) UILabel *titleLable;
@property (nonatomic, strong) UILabel *subLable;

@end

@implementation AbstractBaseERPViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.rightItems = [NSMutableArray array];
        [self setMoreTitleView];
    }
    return self;
}

- (void)setMoreTitleView
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 44)];
    self.navigationItem.titleView = view;
    
    self.titleLable = [[UILabel alloc] initWithFrame:CGRectMake(-100, 0, 200, 30)];
    self.titleLable.textColor = [UIColor whiteColor];
    self.titleLable.textAlignment = NSTextAlignmentCenter;
    self.titleLable.font = [UIFont boldSystemFontOfSize:18.0];
    self.titleLable.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.12];
    self.titleLable.shadowOffset = CGSizeMake(0, -1);
    [view addSubview:self.titleLable];
    
    self.subLable = [[UILabel alloc] initWithFrame:CGRectMake(-100, 26, 200, 14)];
    self.subLable.textColor = [UIColor whiteColor];
    self.subLable.textAlignment = NSTextAlignmentCenter;
    self.subLable.font = [UIFont boldSystemFontOfSize:12.0];
    self.subLable.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.12];
    self.subLable.shadowOffset = CGSizeMake(0, -1);
    [view addSubview:self.subLable];
}

- (void)setStationName:(NSString *)name StationID:(NSInteger)stationID
{
    self.stationID = stationID;
    self.stationName = name;
    self.subLable.text = name;
}

- (void)setStartTime:(NSString *)startTime endTime:(NSString*)endTime
{
    self.startTime = startTime;
    self.endTime = endTime;
}

- (void)setTitle:(NSString *)title
{
    self.titleLable.text = title;
    StationInfoEntity *model = [[GetUserStationService sharedService] getSelectStationEntity];
    self.subLable.text = model.stationName;
}

- (void)setTitle:(NSString *)title subLable:(NSString *)subLable
{
    self.titleLable.text = title;
    self.subLable.text = subLable;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //添加站点变更注册
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateMenuAuthorityService:)
                                                 name:StationInfoChangeKey object:nil];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -
#pragma mark WebView专用

//设置无站点显示
- (void)setWebTitleNotStation:(NSString *)title
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 44)];
    self.navigationItem.titleView = view;
    
    self.titleLable = [[UILabel alloc] initWithFrame:CGRectMake(-100, 0, 200, 44)];
    self.titleLable.textColor = [UIColor whiteColor];
    self.titleLable.textAlignment = NSTextAlignmentCenter;
    self.titleLable.font = [UIFont boldSystemFontOfSize:18.0];
    self.titleLable.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.12];
    self.titleLable.shadowOffset = CGSizeMake(0, -1);
    [view addSubview:self.titleLable];
    
    self.titleLable.text = title;
}

//设置有站点显示
- (void)setWebTitleHaveStation
{
    NSString *titleName = self.titleLable.text;
    [self setMoreTitleView];
    
    self.titleLable.text = titleName;
    StationInfoEntity *model = [[GetUserStationService sharedService] getSelectStationEntity];
    self.subLable.text = model.stationName;
}

#pragma mark -
#pragma mark - NSNotificationCenter

-(void)updateMenuAuthorityService:(NSNotification*)notification
{
    //获取菜单权限表通知
    if ([[notification name] isEqualToString:StationInfoChangeKey]){
        StationInfoEntity *model = [[GetUserStationService sharedService] getSelectStationEntity];
        if (model){
            self.subLable.text = model.stationName;
            [self updateStationNotification:model];
        }
    }
}

-(void)updateStationNotification:(StationInfoEntity*)model
{
    
}

#pragma mark Button Event 打印按钮

- (void)addPrintToNavigationItem
{
    //判断按钮是否绘制，若绘制，就不在绘制该按钮
    for(UIBarButtonItem *button in self.rightItems){
        if(button.tag == 1012) return;
    }
    
    //设置搜索按钮
    UIButton* righButtont= [UIButton buttonWithType:UIButtonTypeCustom];
    righButtont.frame = CGRectMake(0, 0, 36, 44);
    righButtont.titleLabel.adjustsFontSizeToFitWidth = YES;
    righButtont.titleLabel.font = [UIFont boldSystemFontOfSize:16];
    righButtont.titleLabel.textAlignment = NSTextAlignmentLeft|NSTextAlignmentCenter;
    [righButtont setTitle:@"打印" forState:UIControlStateNormal];
    [righButtont setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [righButtont addTarget:self action:@selector(printChargeSelector:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *mButtonItem = [[UIBarButtonItem alloc] initWithCustomView:righButtont];
    mButtonItem.tag = 1012;
    [self.rightItems addObject:mButtonItem];
    self.navigationItem.rightBarButtonItems = self.rightItems;
}

#pragma mark Button Event 搜索切换

- (void)addSearchToNavigationItem
{
    //判断按钮是否绘制，若绘制，就不在绘制该按钮
    for(UIBarButtonItem *button in self.rightItems){
        if(button.tag == 1010) return;
    }
    
    
    //设置搜索按钮
    UIButton* righButtont= [UIButton buttonWithType:UIButtonTypeCustom];
    righButtont.frame = CGRectMake(0, 0, 36, 44);
    [righButtont setImage:[UIImage imageNamed:@"home_nav_left_serch"] forState:UIControlStateNormal];
    [righButtont addTarget:self action:@selector(searchChargeSelector:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *mButtonItem = [[UIBarButtonItem alloc] initWithCustomView:righButtont];
    mButtonItem.tag = 1010;
    [self.rightItems addObject:mButtonItem];
    self.navigationItem.rightBarButtonItems = self.rightItems;
}

#pragma mark Button Event 站点切换

- (void)addStationToNavigationItem
{
    //判断按钮是否绘制，若绘制，就不在绘制该按钮
    for(UIBarButtonItem *button in self.rightItems){
        if(button.tag == 1011)  return;
    }
    
    //初始化站点
    [self initChangeERPStationView];
    
    //添加站点切换，如果存在站点信息就添加切换按钮
    NSArray *menuArray = [[GetUserStationService sharedService] getAllStationToArray];
    if(menuArray && menuArray.count>0){
        UIButton *righButtont = [UIButton buttonWithType:UIButtonTypeCustom];
        righButtont.frame = CGRectMake(0,0,36,44);
        righButtont.titleLabel.adjustsFontSizeToFitWidth = YES;
        righButtont.titleLabel.font = [UIFont boldSystemFontOfSize:16];
        righButtont.titleLabel.textAlignment = NSTextAlignmentLeft|NSTextAlignmentCenter;
        [righButtont setTitle:@"切换" forState:UIControlStateNormal];
        [righButtont setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [righButtont addTarget:self action:@selector(changeERPStation) forControlEvents:UIControlEventTouchUpInside];
        
        UIBarButtonItem *mButtonItem = [[UIBarButtonItem alloc] initWithCustomView:righButtont];
        mButtonItem.tag = 1011;
        [self.rightItems addObject:mButtonItem];
        self.navigationItem.rightBarButtonItems = self.rightItems;
    }
}

- (void)initChangeERPStationView
{
    self.stationMenu = [[REMenu alloc] init];
    self.stationMenu.backgroundColor = [UIColor colorWithRed:246/255.0 green:246/255.0 blue:246/255.0 alpha:1];
    self.stationMenu.separatorColor = [UIColor colorWithWhite:0.85 alpha:1.0];
    self.stationMenu.borderColor = [UIColor colorWithWhite:0.85 alpha:1.0];
    self.stationMenu.font = [UIFont boldSystemFontOfSize:17];
    self.stationMenu.highlightedBackgroundColor = [UIColor colorWithWhite:0.85 alpha:1.0];
    self.stationMenu.highlightedSeparatorColor = [UIColor colorWithWhite:0.85 alpha:1.0];
    
    //添加站点数据
    NSArray *menuArray = [[GetUserStationService sharedService] getAllStationToArray];
    NSMutableArray *menuItemArray = [NSMutableArray array];
    for (StationInfoEntity *entity in menuArray) {
        REMenuItem *homeItem = [[REMenuItem alloc] initWithTitle:entity.stationName subtitle:@"集团站点"
                                                           image:[UIImage imageNamed:@"main_station_icon"]
                                                highlightedImage:nil action:^(REMenuItem *item) {
                                                    [[GetUserStationService sharedService] updataStationEntity:entity];
                                                }];
        [menuItemArray addObject:homeItem];
    }
    self.stationMenu.items = menuItemArray;
}

#pragma mark - UIButton Event
#pragma mark - 以下方法需要，请重写

- (void)changeERPStation
{
    if (self.stationMenu.isOpen){
        return [self.stationMenu close];
    } else {
        [self.stationMenu setBlackBackgroundToView:self.view];
        [self.stationMenu showFromNavigationController:self.navigationController];
    }
}

//搜索按钮方法
- (void)searchChargeSelector:(id)selector
{
    
}

//打印方法
- (void)printChargeSelector:(id)selector
{
    
}

#pragma mark -
#pragma mark IBAction Event
- (void)backButtonEvent:(id)sender
{
    if (_stationMenu && _stationMenu.isOpen) [self.stationMenu close];
    [self.navigationController popViewControllerAnimated:YES];
}

@end
